<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓库编码" prop="stockCode">
        <el-input
          v-model="queryParams.stockCode"
          placeholder="请输入仓库编码模糊查询"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库名称" prop="stockName">
        <el-input
          v-model="queryParams.stockName"
          placeholder="请输入仓库名称模糊查询"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库类别" prop="wmsDictStockTypes">
        <el-select v-model="queryParams.wmsDictStockTypes" multiple  placeholder="请选择仓库类别" clearable>
          <el-option
            v-for="dict in dict.type.wms_dict_stock_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryParams.value" clearable  placeholder="请选择状态">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:stock:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:stock:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:stock:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wms:stock:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="stockList" @selection-change="handleSelectionChange" stripe :highlight-current-row="true">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column fixed label="序号" width="55"   align="center">
        <template scope="scope">
          <span>{{(queryParams.pageNum-1)*queryParams.pageSize+(scope.$index + 1)}} </span>
        </template>
      </el-table-column>
      <el-table-column label="仓库编码" align="center" prop="stockCode" />
      <el-table-column label="仓库名称" align="center" prop="stockName" />
      <el-table-column label="仓管员" align="center" prop="attentionUserName" />
      <el-table-column label="仓库类别" align="center" prop="wmsDictStockType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_dict_stock_type" :value="scope.row.wmsDictStockType"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="sysNormalDisable">
        <template slot-scope="scope">
          <el-tag type="primary" v-if="scope.row.sysNormalDisable=='0'">正常</el-tag>
          <el-tag type="danger"  v-if="scope.row.sysNormalDisable=='1'">停用</el-tag>
          <el-tag type="warning" v-if="scope.row.sysNormalDisable=='盘点中'">盘点中</el-tag>
          <el-tag type="info"    v-if="scope.row.sysNormalDisable=='结帐中'">结帐中</el-tag>
          <el-tag type="success" v-if="scope.row.sysNormalDisable=='初始化'">初始化</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建日期" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="docNote" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:stock:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:stock:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓库管理对话框 -->
    <el-dialog v-dialogDrag v-dialogDragWidth :title="title" :visible.sync="open" width="450px" append-to-body :close-on-click-modal=false>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓库编码" prop="stockCode">
          <el-input v-model="form.stockCode" placeholder="请输入仓库编码" />
        </el-form-item>
        <el-form-item label="仓库名称" prop="stockName">
          <el-input v-model="form.stockName" placeholder="请输入仓库名称" />
        </el-form-item>
        <el-form-item label="仓库类别" prop="wmsDictStockType">
          <el-select v-model="form.wmsDictStockType" placeholder="请选择仓库类别" :style="{width: '100%'}">
            <el-option
              v-for="dict in dict.type.wms_dict_stock_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="fkAttentionUserId" label="仓管员">
          <el-input v-model="form.fkAttentionUserId" v-show="false"/>
          <el-input placeholder="请选择仓管员" v-model="form.attentionUserName" class="input-with-select">
            <el-button @click="showDialog_selectAttentionUser( form )" slot="append"
                       icon="el-icon-search"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="状态" prop="sysNormalDisable">
          <el-select v-model="form.sysNormalDisable" clearable  placeholder="请选择状态" :style="{width: '100%'}">
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="docNote">
          <el-input v-model="form.docNote" type="textarea" autosize placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <select-user ref="component_selectAttentionUser" @ok="selectAttentionUser_done"/>
  </div>
</template>

<script>

import { listStock, getStock, delStock, addStock, updateStock,listStockByWmsDictStockType } from "@/api/wms/stock";
import selectUser from "../../comn/userGroup/selectUser";
import {newDocNum} from "@/api/oa/docNum";
export default {
  name: "Stock",
  dicts: ['wms_dict_stock_type','sys_normal_disable'],
  components: {selectUser},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库表格数据
      stockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stockCode: null,
        stockName: null,
        wmsDictStockTypes:[],
        sysNormalDisable: null,
        docTitle: null,
        docNum: null,
        docDt: null,
        docNote: null,
        fkAttentionUserId: null,
        fkConfirmUserId: null,
        confirmDt: null,
        oaDictConfirmStatus: null,
        confirmNote: null,
        sysYesNoDone: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stockCode: [
          { required: true, message: "编码不能为空", trigger: "change" }
        ],
        stockName: [
          { required: true, message: "仓库名称不能为空", trigger: "change" }
        ],
        wmsDictStockType: [
          { required: true, message: "仓库类别不能为空", trigger: "change" }
        ],
        fkAttentionUserId: [
          { required: true, message: "仓管员不能为空", trigger: "change" }
        ],
        sysNormalDisable: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "change" }
        ],
      },
      options: [{
        value:"0",
        label: '正常'
      }, {
        value:"1",
        label: '停用'
      }, {
        value:"初始化",
        label: '初始化'
      }, {
        value:"盘点中",
        label: '盘点中'
      }, {
        value:"结帐中",
        label: '结帐中'
      }
      ]
    };
  },
  created() {
    this.getList();
  },

  methods: {
    /** 列表序号列自动生成 */
    indexMethod(index){
      return (this.currentPage-1)*this.pageSize+index+1
    },
    /** 查询仓库列表 */
    getList() {
      this.loading = true;
      listStock(this.queryParams).then(response => {
        this.stockList = response.rows;
        console.log(this.stockList)
        this.total = response.total;
        this.loading = false;
      });
    },
    /**取消按钮*/
    cancel() {
      this.open = false;
      this.reset();
    },
    /**表单重置*/
    reset() {
      this.form = {
        stockUuid: null,
        stockCode: null,
        stockName: null,
        wmsDictStockType: null,
        sysNormalDisable: "0",
        docTitle: null,
        docNum: null,
        docDt: null,
        docNote: null,
        fkAttentionUserId: null,
        fkConfirmUserId: null,
        confirmDt: null,
        oaDictConfirmStatus: "0",
        confirmNote: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        sysYesNoDone: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      console.log(this.queryParams)
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /**多选框选中数据*/
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.stockUuid)
      this.stockCodes = selection.map(item => item.stockCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();

      newDocNum({preCode: 'STO-', len: 12}).then(response => {
        this.form.stockCode = response.msg
      })
      this.open = true;
      this.title = "新增仓库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const stockUuid = row.stockUuid || this.ids
      getStock(stockUuid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仓库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.stockUuid != null) {
            updateStock(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStock(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const stockUuids = row.stockUuid || this.ids;
      const stockCodes = row.stockCode || this.stockCodes;
      this.$modal.confirm('是否确认删除仓库编码为"' + stockCodes + '"的数据？','删除确认').then(function() {
        return delStock(stockUuids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/wms/stock/export', {
        ...this.queryParams
      }, `wms_stock.xlsx`)
    },
    /** 打开仓管员选择弹窗 */
    showDialog_selectAttentionUser(form) {
      this.currentForm = form
      this.$refs.component_selectAttentionUser.userGroup = 'warehouse_keeper';
      this.$refs.component_selectAttentionUser.show();
    },

    /** 完成：选择仓管员 */
    selectAttentionUser_done(user) {
      this.currentForm.fkAttentionUserId = user.userId
      this.currentForm.attentionUserName = user.userNickNameAndId
    },
  }
};
</script>
